<?php
namespace app\user\controller;

use app\common\controller\UserBase;
use think\facade\Request;
use think\facade\Db;
use think\facade\Session;

use app\user\model\Customer as Kehu;
use app\common\model\User;
use app\user\model\CustomerTrack;
use app\user\model\CustomerOrder;
use app\user\model\Notice;

class Customer extends UserBase
{
    protected $noAuth = ['json_track','json_order'];
    //客户列表
    public function index()
    {
        $param = $this->request->param();
        $customer = new Kehu();
        if (isset($param['keywords'])) {
            $keywords = $param['keywords'];
            //$where['name'] = ['like', '%$name%'];
            $customer = $customer->where('name','like','%'.$keywords.'%')->whereor('mobile','like','%'.$keywords.'%')->whereor('remark','like','%'.$keywords.'%')->whereor('province','like','%'.$keywords.'%')->whereor('city','like','%'.$keywords.'%');
        }
        if (isset($param['comefrom'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('comefrom',$param['comefrom']);
        }
        if (isset($param['classify'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('classify',$param['classify']);
        }
        if (isset($param['status'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('status',$param['status']);
        }
        if (isset($param['star'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('star',$param['star']);
        }
        if (isset($param['user_id'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('user_id',$param['user_id']);
        }

        $list = $customer->with(['classify','comefrom','status','user'])->order('update_time desc,id desc')->paginate();
        return $this->fetch('index',[
            'list' => $list,
            'action' => Request::action(true),
        ]);
    }
    //我的客户
    public function my_customer()
    {
        $param = $this->request->param();
        $customer = new Kehu();
        if (isset($param['keywords'])) {
            $keywords = $param['keywords'];
            //$where['name'] = ['like', '%$name%'];
            $customer = $customer->where('name','like','%'.$keywords.'%')->whereor('mobile','like','%'.$keywords.'%')->whereor('remark','like','%'.$keywords.'%')->whereor('province','like','%'.$keywords.'%')->whereor('city','like','%'.$keywords.'%');
        }
        if (isset($param['comefrom'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('comefrom',$param['comefrom']);
        }
        if (isset($param['classify'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('classify',$param['classify']);
        }
        if (isset($param['status'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('status',$param['status']);
        }
        if (isset($param['star'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('star',$param['star']);
        }
        //我的客户筛选
        $customer = $customer->where('user_id', UserId());
        $list = $customer->with(['classify','comefrom','status','user'])->order('update_time desc,id desc')->paginate();
        return $this->fetch('index',[
            'list' => $list,
            'action' => Request::action(true),
        ]);
    }
    //重要客户
    public function important()
    {
        $param = $this->request->param();
        $customer = new Kehu();
        if (isset($param['keywords'])) {
            $keywords = $param['keywords'];
            //$where['name'] = ['like', '%$name%'];
            $customer = $customer->where('name','like','%'.$keywords.'%')->whereor('mobile','like','%'.$keywords.'%')->whereor('remark','like','%'.$keywords.'%')->whereor('province','like','%'.$keywords.'%')->whereor('city','like','%'.$keywords.'%');
        }
        if (isset($param['comefrom'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('comefrom',$param['comefrom']);
        }
        if (isset($param['classify'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('classify',$param['classify']);
        }
        if (isset($param['status'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('status',$param['status']);
        }
        //我的客户筛选
        $customer = $customer->where('star','>','3');
        $customer = $customer->where('user_id', UserId());
        $list = $customer->with(['classify','comefrom','status','user'])->order('update_time desc,id desc')->paginate();
        return $this->fetch('index',[
            'list' => $list,
            'action' => Request::action(true),
        ]);
    }
    // 30天未跟进
    public function thirty_days()
    {
        $param = $this->request->param();
        $customer = new Kehu();
        if (isset($param['keywords'])) {
            $keywords = $param['keywords'];
            //$where['name'] = ['like', '%$name%'];
            $customer = $customer->where('name','like','%'.$keywords.'%')->whereor('mobile','like','%'.$keywords.'%')->whereor('remark','like','%'.$keywords.'%')->whereor('province','like','%'.$keywords.'%')->whereor('city','like','%'.$keywords.'%');
        }
        if (isset($param['comefrom'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('comefrom',$param['comefrom']);
        }
        if (isset($param['classify'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('classify',$param['classify']);
        }
        if (isset($param['status'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('status',$param['status']);
        }
        if (isset($param['star'])) {
            //$where['from'] = $param['from'];
            $customer = $customer->where('star',$param['star']);
        }
        //我的客户筛选
        $customer = $customer->where('user_id', UserId());
        //30天之前
        $customer = $customer->whereTime('update_time', '<', 'last month');
        $list = $customer->with(['classify','comefrom','status','user'])->order('update_time desc,id desc')->paginate();
        return $this->fetch('index',[
            'list' => $list,
            'action' => Request::action(true),
        ]);
    }
    //我的客户
    public function detail($id)
    {
        $customer = new Kehu();
        $data = $customer->with(['classify','comefrom','status','user'])->where('id',$id)->find();
        //判断是否处理通知
        $notice = input('notice');
        if($notice){
            Notice::where('id',$notice)->update(['status'=>'1']);
        }
        return $this->fetch('detail',[
            'data' => $data,
        ]);
    }
    //JSON 获取客户回访
    public function json_track($customer_id,$limit)
    {
        $list = CustomerTrack::with(['user'])->where('customer_id',$customer_id)->order('update_time desc,id desc')->paginate($limit);
        $this->result($list);
    }
    //JSON 获取客户交易
    public function json_order($customer_id,$limit)
    {
        $list = CustomerOrder::with(['user','classify'])->where('customer_id',$customer_id)->order('update_time desc,id desc')->paginate($limit);
        $this->result($list);
    }
    
    //新增
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $param['user_id'] = UserId();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'customer');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //验证手机号
            $customer = Kehu::where('mobile',$param['mobile'])->find();
            if($customer){
                $this->error('该手机号码已经存在');
            }
            Kehu::create($param);
            insert_user_log('新增客户:'.$param['name']);
            $this->success('操作成功');
        }
        return $this->fetch('save');
    }
    //编辑
    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'customer');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //验证手机号
            $customer = Kehu::where('mobile',$param['mobile'])->where('id','<>',$param['id'])->find();
            if($customer){
                $this->error('该手机号码已经存在');
            }
            Kehu::update($param,['id'=>$param['id']]);
            insert_user_log('修改了客户资料');
            $this->success('操作成功');
        }
        $data = Kehu::where('id',$id)->find();
        return $this->fetch('save',['data'=>$data]);
    }
    //删除
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Kehu::destroy($param['id']);
            insert_user_log('删除了客户');
            $this->success('删除成功');
        }
    }
    
    //新增回访
    public function track_add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $param['user_id'] = UserId();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'customer_track');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            CustomerTrack::create($param);
            //更新客户信息表
            Kehu::updateTime($param['customer_id']);
            insert_user_log('新增回访');
            $this->success('操作成功');
        }
        return $this->fetch('track_save');
    }
    
    //编辑
    public function track_edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CustomerTrack::update($param,['id'=>$param['id']]);
            insert_user_log('修改了回访记录');
            $this->success('操作成功');
        }
        $data = CustomerTrack::where('id',$id)->find();
        return $this->fetch('track_save',['data'=>$data]);
    }
    //删除回访
    public function track_del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CustomerTrack::destroy($param['id']);
            insert_user_log('删除了回访');
            $this->success('删除成功');
        }
    }
    
    //新增销售
    public function order_add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $param['user_id'] = UserId();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'customer_order');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            CustomerOrder::create($param);
            //更新客户信息表
            Kehu::updateTime($param['customer_id']);
            insert_user_log('新增销售');
            $this->success('操作成功');
        }
        return $this->fetch('order_save');
    }
    
    //编辑销售
    public function order_edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CustomerOrder::update($param,['id'=>$param['id']]);
            insert_user_log('修改了销售记录');
            $this->success('操作成功');
        }
        $data = CustomerOrder::where('id',$id)->find();
        return $this->fetch('order_save',['data'=>$data]);
    }
    //删除销售
    public function order_del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CustomerOrder::destroy($param['id']);
            insert_user_log('删除了销售');
            $this->success('删除成功');
        }
    }
    
}
